package com.cn.jx.manager.service.mtn.Impl;

import java.text.MessageFormat;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.SystemConfigRepository;
import com.cn.jx.manager.dao.SystemOperationRepository;
import com.cn.jx.manager.po.SystemConfig;
import com.cn.jx.manager.po.SystemOperation;
import com.cn.jx.manager.service.mtn.ISystemInfoServiceMtn;
import com.jx.util.MapObject;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("systemInfoServiceMtn") 
@Transactional
public class SystemInfoServiceImplMtn extends BaseService implements ISystemInfoServiceMtn {
	
	/**定义{@link SystemOperationRepository systemOperationDao <tt>系统操作Dao层接口定义</tt>}*/
	@Autowired
	private SystemOperationRepository systemOperationDao;
	
	@Autowired
	private SystemConfigRepository systemConfigDao;

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveOperLog(SystemOperation op) {
		systemOperationDao.save(op);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updataSystemConfig(String column,String value) {
		String sql=" update SystemConfig set {0}=:val ";
		Query query=entityManager.createQuery(MessageFormat.format(sql, column));
		MapObject mo=getMap(column,value);
		query.setParameter("val", mo.get(column));
		query.executeUpdate();
	}
	
	private MapObject getMap(String column,String value) {
		MapObject mo=MapObject.getInstance();
		switch (column) {
		case "headHeight":
			try {
				Integer te=Integer.parseInt(value);
				mo.put(column, te);
			} catch (NumberFormatException e) {
				mo.put(column, "");
			}
			break;
		default:
			mo.put(column, value);
			break;
		}
		return mo;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void initSystemConfig() {
		SystemConfig fg=SystemConfig.getInstance();
		fg.setHeadMainTitle("后台管理系统");
		fg.setHeadSubTitle("Enterprise Information Manage System");
		fg.setHeadLogo("/images/logoImg.png");
		fg.setHeadHeight(80);
		fg.setFootCopyright("© 2019 作者:姜旭   版权所有");
		fg.setFootHeight(30);
		fg.setNavigatIcons("icon-base");
		fg.setNavigatWidth(200);
		fg.setNavigatTitle("导航");
		fg.setInformationBarIcons("icon-cog");
		fg.setInformationBarTitle("系统信息");
		fg.setInformationBarWidth(280);
		fg.setMainPanelIcons("icon-house");
		fg.setMainPanelTitle("欢迎使用旭日后台管理系统");
		fg.setWelcomeIcons("icon-shield");
		fg.setWelcomeTitle("首页");
		systemConfigDao.save(fg);
	}

}
